<?php
/**
 * 会员注册
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class registerControl extends MobileControl
{

    public function __construct()
    {
        parent::__construct();
    }

    public function indexOp()
    {
        //实例化模型
        $model_member = new memberModel();
        //检查登录状态
        $model_member->checkloginMember();
        //注册提交
        if (chksubmit()) {
            registerModel::CodeReg();

        }
        if (C('captcha_status_register') == '1') {
            Tpl::output('nchash', substr(md5(SiteUrl . $_GET['act'] . $_GET['op']), 0, 8));
        }
        Tpl::output('html_title', C('site_name') . ' - 会员注册');
        Tpl::output('code_reg', true);
        Tpl::showpage('register');
    }

    private function upload($file, $path)
    {
        if ($_FILES [$file] ["error"] > 0) {
            return false;
        }
        $fileExt = pathinfo($_FILES [$file] ['name'], PATHINFO_EXTENSION);
        if (!in_array(strtolower($fileExt), array('jpg', 'jpeg', 'gif', 'png'))) {
            return false;
        }
        $dir = BasePath . '/' . $path;
        @mkdir($dir, 0777, true);
        while (true) {
            $fileName = date("YmdHis") . str_pad(rand(0, 99), 2, 0, STR_PAD_LEFT) . '.' . $fileExt;
            $fileUrl = $path . $fileName;
            $filePath = $dir . $fileName;
            if (!is_file($filePath)) {
                break;
            } else {
                sleep(1);
            }
        }
        if (!move_uploaded_file($_FILES [$file] ['tmp_name'], $filePath)) {
            return false;
        }
        return $fileUrl;
    }

    //用户服务协议
    public function doc_contentOp()
    {
        $model_doc = new documentModel();
        $doc = $model_doc->getOneByCode('agreement');
        Tpl::output('doc', $doc);
        Tpl::showpage('doc_content');
    }

    public function no_code_regOp()
    {
        $model_member = new memberModel();
        $model_member->checkloginMember();
        if (chksubmit()) {
            registerModel::noCodeReg();
        }
        if (C('captcha_status_register') == '1') {
            Tpl::output('nchash', substr(md5(SiteUrl . $_GET['act'] . $_GET['op']), 0, 8));
        }
        Tpl::output('html_title', C('site_name') . ' - 会员注册');
        Tpl::showpage('register');
    }

    //验证用户名是否存在
    public function checkNameOp()
    {
        $model_member = new memberModel();
        $check_member_name = $model_member->infoMember(array('member_name' => trim($_GET['user_name'])));
        if (is_array($check_member_name) && count($check_member_name) > 0) {
            exit('false');
        } else {
            exit('true');
        }
    }

    //手机号码检测
    public function checkMobileOp()
    {
        $model_member = new memberModel();
        $check_mobile_name = $model_member->infoMember(array('member_mobile' => trim($_GET['mobile'])));
        if (is_array($check_mobile_name) and count($check_mobile_name) > 0) {
            echo 'false';
        } else {
            echo 'true';
        }
    }

    // 注册发送新手机验证码
    public function ajax_get_code_reg_mobileOp()
    {
        //2015-11-21整理
        registerModel::get_code_reg_mobile();exit;
    }

    /**
     * 注册验证手机验证码
     */
    public function ajax_check_mobcodeOp()
    {
        //2015-11-21整理
        registerModel::checkMobileCode();exit;
    }

    //ajax验证邀请码有效性
    public function checkCodeOp()
    {
        //2015-11-21整理
        registerModel::checkCode();exit;
    }

    //产生随机店名后缀
    private function create_random($store_name)
    {
        $name = null;
        while (true) {
            $num = range('0', '9');
            shuffle($num);//打乱数组
            $key = array_rand($num, 3);//随机取3个单元元素
            $str = '';
            foreach ($key as $v) {
                $str .= $num[$v];
            }
            $name = $store_name . '_' . $str;
            $sql = "SELECT * FROM #__member WHERE member_name = '$name'";
            if (!db::queryRow($sql)) {
                break;
            } else {
                sleep(1);
            }
        }
        return $name;
    }

    //手机端验证类型判断,选择哪种注册方案就用对哪种照片进行验证
    private function mv_type($type)
    {
        if ($type == $_POST['mv_type']) {
            return true;
        }
        return false;
    }

    //验证邀请会员是否存在,如果不存在，则返回false，客户端作出错误提示
    public function checkInviteNameOp()
    {
        $model_member = new memberModel();
        $check_member_name = $model_member->infoMember(array('member_name' => trim($_GET['invite_name'])), 'member_name');
        if (is_array($check_member_name) && count($check_member_name) > 0) {
            exit('true');
        } else {
            exit('false');
        }
    }
}